<?php
class Members extends UaModel {
	
	var $name = "Member";
	
	var $ua_positions = array (0 => "请选择", 1 => "董事长、总裁及副职，企业主、企业合伙人，总经理/副总经理", 2 => "行政部门经理/行政人员", 3 => "技术部门经理/技术人员", 4 => "生产部门经理/生产人员", 5 => "市场部门经理/市场人员", 6 => "采购部门经理/采购人员", 7 => "销售部门经理/销售人员", 8 => "其他" );
	var $ua_member_types = array (0 => "会员类型", 1 => "免费企业会员", 2 => "收费企业会员" );
	var $mask_user_name = "admin";
	var $genders = array ("1" => "先生", "2" => "女士", "0" => "保密" );
	var $member_status = array ("0" => "无效", "1" => "有效", "2" => "等待审核", "3" => "审核不通过", "4" => "被禁用" );
	var $im_types = array ("1" => "QQ", "2" => "ICQ", "3" => "Msn Messenger", "4" => "Yahoo Messenger", "5" => "Skype" );
	var $phone_types = array ("1" => "移动电话", "2" => "住宅电话", "3" => "商务电话", "4" => "其他电话" );
	
	function Members() {
	
	}
	
	function checkUserLogin($uname, $upass) {
		global $time_stamp;
		global $memberlog;
		$uname = uaAddslashes ( $uname );
		$upass = uaAddslashes ( $upass );
		$sql = "SELECT id AS UserID,username AS LoginName,userpass AS LoginPass,status AS MemberStatus FROM " . $this->getTable ( true ) . " WHERE username='$uname'";
		$tmpUser = $GLOBALS ['g_db']->GetRow ( $sql );
		$true_pass = $tmpUser ['LoginPass'];
		$loginip = uaIp2Long ( uaGetClientIP () );
		$sql = "insert into " . $memberlog->getTable () . " (member_id,login_time,login_ip) values (" . $tmpUser ['UserID'] . ",'$time_stamp','$loginip')";
		$GLOBALS ['g_db']->Execute ( $sql );
		$GLOBALS ['g_db']->Execute ( "UPDATE " . $this->getTable () . " SET last_login=" . $time_stamp . " WHERE username='$uname'" );
		if (empty ( $uname ) || empty ( $upass )) {
			return - 1;
		} elseif (! $this->checkUserExist ( $uname )) {
			return - 2;
		} elseif (strcmp ( $true_pass, md5 ( $upass ) ) != 0) {
			return - 3;
		} elseif ($tmpUser ['MemberStatus'] != 1) {
			return - 4;
		} else {
			return true;
		}
	}
	
	function checkUserExist($uname) {
		$uname = uaAddSlashes ( $uname );
		$sql = "SELECT username FROM " . $this->getTable ( true ) . " WHERE username='$uname'";
		$tmp_exists = $GLOBALS ['g_db']->GetOne ( $sql );
		if (! empty ( $tmp_exists ) || $tmp_exists != '') {
			return true;
		} else {
			return false;
		}
	}
	
	function getMemberInfo($username) {
		$uname = uaAddSlashes ( $username );
		$sql = "SELECT * FROM " . $this->getTable ( true ) . " WHERE username='$uname'";
		$tmpinfo = $GLOBALS ['g_db']->GetRow ( $sql );
		if (! empty ( $tmpinfo ) || $tmpinfo != '') {
			return $tmpinfo;
		} else {
			return false;
		}
	}
	
	function getMemberInfoByID($userid, $cols = null) {
		$uid = trim ( $userid );
		$sql = "SELECT ";
		if (empty ( $cols ))
			$sql .= "*";
		else
			$sql .= $cols;
		$sql .= " FROM " . $this->getTable ( true ) . " AS Member WHERE Member.id='$uid'";
		$tmpinfo = $GLOBALS ['g_db']->GetRow ( $sql );
		if (! empty ( $tmpinfo ) || $tmpinfo != '') {
			return $tmpinfo;
		} else {
			return false;
		}
	}
	
	function updateUserStatus($id_array, $status = 1) {
		global $g_db;
		if (is_array ( $id_array )) {
			$tmp_ids = implode ( ",", $id_array );
			$sql = "update " . $this->getTable () . " set status='$status' where id in (" . $tmp_ids . ")";
		} else {
			$sql = "update " . $this->getTable () . " set status='$status'  WHERE id=" . intval ( $id_array );
		}
		$result = $g_db->Execute ( $sql );
		if ($result) {
			return true;
		} else {
			return false;
		}
	}
}
?>